package com.jiezhuo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.RoleMenu;
import com.jiezhuo.entity.UserRole;
import com.jiezhuo.service.RoleMenuService;
import com.jiezhuo.service.UserRoleService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2022-10-05
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController {
    @Autowired
    UserRoleService userRoleService;
    @ApiOperation("添加角色菜单关联")
    @PostMapping("saveCategory")
    public R saveOrUpdateCategory(String userId , String[] roleIds){
        ArrayList<UserRole> roleMenus = new ArrayList<>();
        QueryWrapper<UserRole> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",userId);
        userRoleService.getBaseMapper().delete(objectQueryWrapper);
        for (int i = 0; i < roleIds.length; i++) {
            UserRole roleMenu = new UserRole();
            roleMenu.setRoleId(userId);
            roleMenu.setUserId(roleIds[i]);
            roleMenus.add(roleMenu);
        }
        boolean b = userRoleService.saveBatch(roleMenus);
        if(b) {
            return R.ok().msg("添加成功");
        }
        return R.ok().msg("添加失败");
    }

    @ApiOperation("删除角色菜单关联")
    @PostMapping("removeCategory")
    public R removeCategory(String userId){
        QueryWrapper<UserRole> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",userId);
        int delete = userRoleService.getBaseMapper().delete(objectQueryWrapper);

        return R.ok().msg("删除成功");
    }

}

